<?php
namespace app\customer\controller;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Set extends Base{
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
    //系统设置
    public function system_set(){
        if(request()->isGet()){
            $data=Db::name('systemset')->where('cus_id',session('customer')['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['web_title']=$param['web_title'];
            $data['web_intro']=$param['web_intro'];
            $data['img']=$param['img'];
            $data['seo_keyword']=$param['seo_keyword'];
            $data['seo_desc']=$param['seo_desc'];
            $data['open_comment']=$param['open_comment'];
            $data['check_comment']=$param['check_comment'];
            $data['comment_titmes']=$param['comment_titmes'];
            $data['share_title']=$param['share_title'];
            $data['share_img']=$param['share_img'];
            $data['email']=$param['email'];
            $data['from_email']=$param['from_email'];
            $data['email_code']=$param['email_code'];
            $data['footer_show']=$param['footer_show'];
            $data['footer_use']=$param['footer_use'];
            $data['create_time']=time();
            $data['update_time']=time();
            $res= Db::name('systemset')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                $cus=Db::name('systemset')->where('cus_id',session('customer')['id'])->find();
                if($cus){
                    return json(['code' => 500,'msg' => '失败']);
                }else{
                    $resin= Db::name('systemset')->insert($data);
                    if($resin){
                        return json(['code' => 200,'msg' => '保存成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }
            }
        }
    }
    //主题色
    public function themecolor(){
        if(request()->isGet()){
            $data=Db::name('customer')->where('id',session('customer')['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['backcolor']=$param['backcolor'];
            $data['themecolor']=$param['themecolor'];
            $data['themefont']=$param['themefont'];
            $data['bottomcolor']=$param['bottomcolor'];
            $data['pcnavcolor']=$param['pcnavcolor'];
            $data['pcnavfontcolor']=$param['pcnavfontcolor'];
            $data['pcnavfontscolor']=$param['pcnavfontscolor'];
            $data['pcthemecolor']=$param['pcthemecolor'];
            $data['pcbottomcolor']=$param['pcbottomcolor'];
            $data['pcbottomfontcolor']=$param['pcbottomfontcolor'];
            $data['search_state']=$param['search_state'];
            $data['bannerw']=$param['bannerw'];
            $data['bannerh']=$param['bannerh'];
            $data['inavs']=$param['inavs'];
            $data['bnavh']=$param['bnavh'];
            $data['bnavs']=$param['bnavs'];
            $data['uptime']=time();
            $res= Db::name('customer')->where(array('id'=>session('customer')['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    //模块设置
    public function module_set(){
        $list=Db::name('moduleset')->where('cus_id',session('customer')['id'])->order('id asc')->paginate(100);
        $lists=Db::name('moduleset')->where('cus_id',session('customer')['id'])->select();
        $data=$list->all();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    //模块修改
    public function submitname(){
        $id=input('param.id');
        $status = Db::name('moduleset')->where(array('id'=>$id))->update(['name'=>input('param.name')]);
        if($status){
            return json(['code' => 200,'msg' => '修改成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    //模块修改
    public function submitename(){
        $id=input('param.id');
        $status = Db::name('moduleset')->where(array('id'=>$id))->update(['e_name'=>input('param.e_name')]);
        if($status){
            return json(['code' => 200,'msg' => '修改成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    //模块设置--禁用
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('moduleset')->where(array('id'=>$id))->update(['isshow'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    //模块设置--开启
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('moduleset')->where(array('id'=>$id))->update(['isshow'=>0]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    //关于我们
    public function aboutus(){
        if(request()->isGet()){
            $data=Db::name('aboutus')->where('cus_id',session('customer')['id'])->find();
            if($data){
                $data['call_more'] = json_decode($data['call_more'],true);
            }
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['name']=$param['name'];
            $data['seotitle']=$param['seotitle'];
            $data['seokey']=$param['seokey'];
            $data['seodes']=$param['seodes'];
            $data['intro']=$param['intro'];
            $data['slogen']=$param['slogen'];
            $data['logo']=$param['logo'];
            $data['thumb_img']=$param['thumb_img'];
            if($param['video']!=''){
                $data['video']=$param['video'];
            }else{
                if(isset($param['videob'])){
                    $data['video']=$param['videob'];
                }
            }
            $data['telephone']=$param['telephone'];
            $data['mobile']=$param['mobile'];
            $data['email']=$param['email'];
            $data['technology']=$param['technology'];
            $data['industryexpertise']=$param['industryexpertise'];
            $data['employees']=$param['employees'];
            $data['patent']=$param['patent'];
            $data['address']=$param['address'];
            $data['traffic']=$param['traffic'];
            $data['coordinates']=$param['coordinates'];
            $data['record']=$param['record'];
            if(isset($param['about'])){
                $data['about']=html_entity_decode($param['about']);
            }
            $data['ico']=$param['ico'];
            $data['fax']=$param['fax'];
            $data['wqrcode']=$param['wqrcode'];
            $data['qqrcode']=$param['qqrcode'];
            $data['miit']=$param['miit'];
            $data['psr']=$param['psr'];
            $data['update_time']=time();
            $orther = [];
            for($i = 0; $i < 10; $i++)
            {
                $elem = 'label'.$i;
                $num = 'number'.$i;
                if(isset($param[$elem]) && isset($param[$num]) && $param[$elem] != '' && $param[$num] != '')
                {
                    $tem = [
                      'name' =>  $param[$elem],
                      'number' => $param[$num]
                    ];
                   array_push($orther, $tem);
                } 
            }
            $data['call_more'] = json_encode($orther);
            $res= Db::name('aboutus')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                $cus=Db::name('aboutus')->where('cus_id',session('customer')['id'])->find();
                if($cus){
                    return json(['code' => 500,'msg' => '失败']);
                }else{
                    $resin= Db::name('aboutus')->insert($data);
                    if($resin){
                        return json(['code' => 200,'msg' => '保存成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }
            }
        }
    }
    //小程序设置
    public function wechat_set(){
        if(request()->isGet()){
            $data=Db::name('aboutus')->where('cus_id',session('customer')['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['appid']=$param['appid'];
            $data['appsecret']=$param['appsecret'];
            $data['ttappid']=$param['ttappid'];
            $data['ttappsecret']=$param['ttappsecret'];
            $data['bdappid']=$param['bdappid'];
            $data['bdappkey']=$param['bdappkey'];
            $data['bdappsecret']=$param['bdappsecret'];
            $res= Db::name('aboutus')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                $cus=Db::name('aboutus')->where('cus_id',session('customer')['id'])->find();
                if($cus){
                    return json(['code' => 500,'msg' => '失败']);
                }else{
                    $resin= Db::name('aboutus')->insert($data);
                    if($resin){
                        return json(['code' => 200,'msg' => '保存成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }
            }
        }
    }
    //表单预约
    public function table_set(){
        if(request()->isGet()){
            $param = input('get.');
            $v=Db::name('tableset')->where('cus_id',session('customer')['id'])->find();
            if($v){
                $v['taconfig'] = json_decode($v['taconfig'],true);
                $v['txtconfig'] = json_decode($v['txtconfig'],true);
                $v['iaconfig'] = json_decode($v['iaconfig'],true);
                $v['icconfig'] = json_decode($v['icconfig'],true);
            }
            $this->assign('v',$v);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['index_show']=$param['index_show'];
            $data['form_title']=$param['form_title'];
            $data['form_img']=$param['form_img'];
            $data['form_intro']=$param['form_intro'];
            $data['form_time']=$param['form_time'];
            if($param['email_state']==1){
                if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$param['email_self'])){
                    return json(['code' => 501,'msg' => '邮箱格式不正确']);
                }
                if($param['email_code']==''){
                    return json(['code' => 502,'msg' => '请填写授权码']);
                }
            }
            $data['email_state']=$param['email_state'];
            $data['email_self']=$param['email_self'];
            $data['email_code']=$param['email_code'];
            $data['taconfig']=json_encode([ // 多行文本设置
                'title' =>$param['aname'],
                'show' =>isset($param['ashow'])?1:0,
                'isnull' =>isset($param['afull'])?1:0
            ]);
            $data['txtconfig']=array( // 单行文本设置
                array(
                  'title' => $param['t1name'],
                  'show' =>isset($param['t1show'])?1:0,
                  'isnull' =>isset($param['t1full'])?1:0,
                  'email' =>isset($param['t1email'])?1:0,
                  'phone' =>isset($param['t1phone'])?1:0
                ),
                array(
                  'title' =>$param['t2name'],
                  'show' =>isset($param['t2show'])?1:0,
                  'isnull' =>isset($param['t2full'])?1:0,
                  'email' =>isset($param['t2email'])?1:0,
                  'phone' =>isset($param['t2phone'])?1:0
                ),
                array(
                  'title' => $param['t3name'],
                  'show' =>isset($param['t3show'])?1:0,
                  'isnull' =>isset($param['t3full'])?1:0,
                  'email' =>isset($param['t3email'])?1:0,
                  'phone' =>isset($param['t3phone'])?1:0
                ),
                array(
                  'title' =>$param['t4name'],
                  'show' =>isset($param['t4show'])?1:0,
                  'isnull' =>isset($param['t4full'])?1:0,
                  'email' =>isset($param['t4email'])?1:0,
                  'phone' =>isset($param['t4phone'])?1:0
                ),
            );
            $data['txtconfig'] = json_encode( $data['txtconfig']);
            $data['iaconfig']=json_encode([ // 单选框设置
              'title' =>$param['rname'],
              'show' =>isset($param['rshow'])?1:0,
              'isnull' =>isset($param['rfull'])?1:0,
              'value' =>$param['rvalue']
            ]);
            $data['icconfig']=json_encode([ // 多选框设置
              'title' => $param['cname'],
              'show' =>isset($param['cshow'])?1:0,
              'isnull' =>isset($param['cfull'])?1:0,
              'value' =>$param['cvalue']
            ]);
            $data['update_time'] =time();
            $res= Db::name('tableset')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                $cus=Db::name('tableset')->where('cus_id',session('customer')['id'])->find();
                if($cus){
                    return json(['code' => 500,'msg' => '失败']);
                }else{
                    $data['create_time'] =time();
                    $resin= Db::name('tableset')->insert($data);
                    if($resin){
                        return json(['code' => 200,'msg' => '保存成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }
            }
        }
    }
    public function programlink(){
        $customer=Db::name('customer')->where('id',session('customer')['id'])->find();
        $this->assign('customer',$customer);
        return $this->fetch();

    }
    //视频上传
    public function uploadv(){
        $file=$_FILES['file'];
        $imgpath="../public/uploads/video/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
        return json(['code' => 200, 'data' =>"/uploads/video/".$imgsubpath.$file['name'], 'msg' => '成功']);
    }
    // 原图上传
    public function upload(){
        $file=$_FILES['file'];
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
        return json(['code' => 200, 'data' =>"/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
    }
    //压缩图片并上传
    public function uploads(){
        header("Content-type: image/jpeg");
        $files=$_FILES['file'];
        $file = $files['tmp_name']; 
        $percent = 1; //图片压缩比 
        list($width, $height) = getimagesize($file); //获取原图尺寸 
        //缩放尺寸 
        $newwidth = $width * $percent; 
        $newheight = $height * $percent; 
        if(getimagesize($file)[2]==2){
           $src_im = imagecreatefromjpeg($file);
        }elseif(getimagesize($file)[2]==3){
            $src_im = imagecreatefrompng($file);
        }
        $dst_im = imagecreatetruecolor($newwidth, $newheight); 
        imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        imagejpeg($dst_im,$imgpath.$imgsubpath.$files['name']); //输出压缩后的图片 
        imagedestroy($dst_im); 
        imagedestroy($src_im);
        return json(['code' => 200, 'data' =>"/uploads/images/".$imgsubpath.$files['name'], 'msg' => '成功']);
    }
    public function tmdedit(){//修改
        $cus_id=session('customer')['id'];
        $siteroot='siteroot'."':'".config('tmdhttp').$_SERVER['HTTP_HOST'];
        $cus=Db::name('aboutus')->where('cus_id',session('customer')['id'])->find();
        if ($cus['appid']) {
            $appid = file_get_contents("./download/project.config.json");
            $appidjson = str_replace(explode(':',explode(',', $appid)[5])[1],'"'.$cus['appid'].'"', $appid);
            file_put_contents("./download/project.config.json", $appidjson);
            $cusid = file_get_contents("./download/siteinfo.js");
            $update_cusid = str_replace(explode(':',explode(',', $cusid)[0])[1],"'".$cus_id."'", $cusid);
            file_put_contents("./download/siteinfo.js", $update_cusid);
            $site = file_get_contents("./download/siteinfo.js");
            $update_site = str_replace(explode(',', $site)[1],"'".$siteroot."'", $site);
            // $update_site = str_replace(explode(':',explode(',', $site)[1])[1],"'".$siteroot."'", $site);
            file_put_contents("./download/siteinfo.js", $update_site);
            $tmdzip=$this->tmdzip();
            return json(['code' => 200,'data'=>$tmdzip,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => 'appid']);
        }
    }
    private function tmdzip(){//覆盖
        $origin_str = file_get_contents("./download/siteinfo.js");
        $appidjsons = file_get_contents("./download/project.config.json");
        $zip=new \ZipArchive;
        $res=$zip->open('./download/mp-weixin.zip',\ZipArchive::CREATE);
        if($res===TRUE){
            $zip->addFromString('mp-weixin/siteinfo.js',$origin_str);
            $zip->addFromString('mp-weixin/project.config.json',$appidjsons);
            $zip->close();
            return 200;
        }else {
            return 500;
        }
    }
    public function tmdeditbd(){//修改
        $cus_id=session('customer')['id'];
        $siteroot='siteroot'."':'".config('tmdhttp').$_SERVER['HTTP_HOST'];
        $cus=Db::name('aboutus')->where('cus_id',session('customer')['id'])->find();
        if ($cus['bdappid']) {
            $appid = file_get_contents("./download/project.swan.json");
            $appidjson = str_replace(explode(':',explode(',', $appid)[1])[1],'"'.$cus['bdappid'].'"', $appid);
            file_put_contents("./download/project.swan.json", $appidjson);
            $cusid = file_get_contents("./download/siteinfo.js");
            $update_cusid = str_replace(explode(':',explode(',', $cusid)[0])[1],"'".$cus_id."'", $cusid);
            file_put_contents("./download/siteinfo.js", $update_cusid);
            $site = file_get_contents("./download/siteinfo.js");
            $update_site = str_replace(explode(',', $site)[1],"'".$siteroot."'", $site);
            file_put_contents("./download/siteinfo.js", $update_site);
            $tmdzip=$this->tmdzipbd();
            return json(['code' => 200,'data'=>$tmdzip,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => 'appid']);
        }
    }
    private function tmdzipbd(){//覆盖
        $origin_str = file_get_contents("./download/siteinfo.js");
        $appidjsons = file_get_contents("./download/project.swan.json");
        $zip=new \ZipArchive;
        $res=$zip->open('./download/mp-baidu.zip',\ZipArchive::CREATE);
        if($res===TRUE){
            $zip->addFromString('mp-baidu/siteinfo.js',$origin_str);
            $zip->addFromString('mp-baidu/project.swan.json',$appidjsons);
            $zip->close();
            return 200;
        }else {
            return 500;
        }
    }
    public function tmdedittt(){//修改
        $cus_id=session('customer')['id'];
        $siteroot='siteroot'."':'".config('tmdhttp').$_SERVER['HTTP_HOST'];
        $cus=Db::name('aboutus')->where('cus_id',session('customer')['id'])->find();
        if ($cus['ttappid']) {
            $appid = file_get_contents("./download/projectt.config.json");
            $appidjson = str_replace(explode(':',explode(',', $appid)[5])[1],'"'.$cus['ttappid'].'"', $appid);
            file_put_contents("./download/projectt.config.json", $appidjson);
            $cusid = file_get_contents("./download/siteinfo.js");
            $update_cusid = str_replace(explode(':',explode(',', $cusid)[0])[1],"'".$cus_id."'", $cusid);
            file_put_contents("./download/siteinfo.js", $update_cusid);
            $site = file_get_contents("./download/siteinfo.js");
            $update_site = str_replace(explode(',', $site)[1],"'".$siteroot."'", $site);
            file_put_contents("./download/siteinfo.js", $update_site);
            $tmdzip=$this->tmdziptt();
            return json(['code' => 200,'data'=>$tmdzip,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => 'appid']);
        }
    }
    private function tmdziptt(){//覆盖
        $origin_str = file_get_contents("./download/siteinfo.js");
        $appidjsons = file_get_contents("./download/projectt.config.json");
        $zip=new \ZipArchive;
        $res=$zip->open('./download/mp-zjtd.zip',\ZipArchive::CREATE);
        if($res===TRUE){
            $zip->addFromString('mp-zjtd/siteinfo.js',$origin_str);
            $zip->addFromString('mp-zjtd/project.config.json',$appidjsons);
            $zip->close();
            return 200;
        }else {
            return 500;
        }
    }
    // public function tmdreadzip(){//读
    //     $zipPath = 'zip://./download/mp-weixin.zip#mp-weixin/project.config.json';
    //     $fp = fopen($zipPath,'rb'); // 打开文件
    //     if($fp !== false){
    //         // 输出csv中所有的行，直到文件结束为止。
    //         while(!feof($fp)){  // feof 检测是否已到达文件末尾 返回TRUE或者FALSE
    //             $data[] = fgetcsv($fp);
    //         }
    //         var_dump($data);die;
    //     }
    //     fclose($fp); // 关闭打开的文件
    // }
}
